﻿using System.Web.Routing;

namespace CodeContrib.Web.Routing
{
	public static class RouteDebugger
	{
		public static void RewriteRoutes(RouteCollection routes)
		{
			using (routes.GetReadLock())
			{
				var foundDebugRoute = false;

				foreach (var routeBase in routes)
				{
					var route = routeBase as Route;

					if (route != null)
					{
						route.RouteHandler = new RouteDebuggerRouteHandler();
					}

					if (route == DebugRoute.Instance)
					{
						foundDebugRoute = true;
					}
				}

				if (!foundDebugRoute)
				{
					routes.Add(DebugRoute.Instance);
				}
			}
		}
	}
}